<template>
  <div class="flex gap-x-[20px]">
    <template v-for="option in options" :key="option.key">
      <div
        class="option-item rounded-3xl cursor-pointer"
        :class="{ active: option.key === innerPlatform }"
        @click="handleClick(option.key)"
      >
        <span class="text-xl font-semibold">{{ option.title }}</span>
      </div>
    </template>
  </div>
</template>
<script lang="ts" setup>
import { ref, watch } from 'vue'

interface Props {
  platform: string
}
interface Options {
  key: string
  title: string
  icon: string
}
const props = withDefaults(defineProps<Props>(), {
  platform: 'douyin',
})
const emit = defineEmits<{
  'update:platform': [value: string]
  refresh: []
}>()

const innerPlatform = ref<string>(props.platform)

const options = ref<Options[]>([
  {
    key: 'douyin',
    title: '抖音',
    icon: '',
  },
  {
    key: 'xiaohongshu',
    title: '小红书',
    icon: '',
  },
])

// 同步父组件传入的 platform 到内部状态，保证外部修改时高亮正确
watch(
  () => props.platform,
  (newVal) => {
    innerPlatform.value = newVal
  },
)

const handleClick = (key: string) => {
  innerPlatform.value = key
  emit('update:platform', key)
  emit('refresh')
}
</script>

<style lang="scss" scoped>
.option-item {
  width: 138px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  background: linear-gradient(3deg, #ffffff 0%, #e2ebff 100%);
  box-shadow: 0px 2px 6px 0px rgba(170, 179, 195, 0.14);
  border: 2px solid #fff;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.option-item:hover {
  border: 2px solid #1677ff;
}
.active {
  position: relative;
  border-color: #1677ff;
  border-width: 2px;
  border-style: solid;
  &::after {
    position: absolute;
    content: '';
    left: 50%;
    bottom: -15px;
    width: 0;
    height: 0;
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-top: 15px solid #1677ff; // 外层蓝色三角形
    transform: translateX(-50%);
    z-index: 1;
  }
  &::before {
    position: absolute;
    content: '';
    left: 50%;
    bottom: -12px; // 稍微上移以露出外层边框
    width: 0;
    height: 0;
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    border-top: 12px solid #ffffff; // 内层白色三角形，形成中空效果
    transform: translateX(-50%);
    z-index: 2;
  }
}
</style>
